/**
 * @Description: TODO
 * @author Jiajun,Ding
 * @date 2016年2月3日 下午11:28:49
 * @version V1.0
 */
package com.cchr.acms.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cchr.acms.util.MessageUtil;

import net.sf.json.JSONObject;

/**
 * @Description: 日志切入类
 * @author Jiajun,Ding
 * @date 2016年3月22日 下午11:28:49
 * @version V1.0
 */
public class LogAspect {

    private static final Logger logger = LoggerFactory.getLogger("controllerLogger");
    private static final Logger exceptionllogger = LoggerFactory.getLogger("exceptionLogger");

    public Object aroundContorller(ProceedingJoinPoint joinPoint) {
        Object result = null;
        String methodName = joinPoint.getTarget().getClass().getName() + "."
                + joinPoint.getSignature().getName();

        Object[] args = new Object[2];
        args[0] = methodName;
        try {
            if (joinPoint.getArgs() != null) {
                StringBuilder sb= new StringBuilder();
                for (Object str : joinPoint.getArgs()) {
                    if (str != null) {
                        sb.append(str.toString()).append("-");
                    }
                }
                String params = sb.toString();
                if (params.endsWith("-")) {
                    params = params.substring(0, params.lastIndexOf("-"));
                }
                args[1] = params;
            }
            logger.info(MessageUtil.getMessage("sys.controller.start.log", args));

            result = joinPoint.proceed();
            if (result != null) {
                args[1] = JSONObject.fromObject(result);
            } else {
                args[1] = null;
            }
            logger.info(MessageUtil.getMessage("sys.controller.end.log", args));
        } catch (Throwable ex) {
            exceptionllogger.error(methodName, ex);
        }

        return result;
    }
}
